package com.study.jyg.rxjava2study.activity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.widget.SeekBar;
import android.widget.TextView;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.widget.LineChart;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 折线图
 */
public class LineChartActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.line_chart)
    LineChart mLineChart;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.sb_one)
    SeekBar sbOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.sb_two)
    SeekBar sbTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.sb_three)
    SeekBar sbThree;
    @BindView(R.id.tv_four)
    TextView tvFour;
    @BindView(R.id.sb_four)
    SeekBar sbFour;
    @BindView(R.id.tv_five)
    TextView tvFive;
    @BindView(R.id.sb_five)
    SeekBar sbFive;

    private ArrayList<Float> sumData;
    private List<String> seletcMonths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        ButterKnife.bind(this);
        toolBar.setNavigationOnClickListener(v -> finish());
        initData();
    }

    private void initData() {
        sumData = new ArrayList<>();
        seletcMonths = new ArrayList<>();
        for (int i = 0; i < 112; i++) {
            seletcMonths.add((i % 12 + 1) + "点");
            sumData.add(getProgress(i));
        }
        mLineChart.setSumData(sumData, seletcMonths);

        tvOne.setText(seletcMonths.get(0));
        tvTwo.setText(seletcMonths.get(1));
        tvThree.setText(seletcMonths.get(2));
        tvFour.setText(seletcMonths.get(3));
        tvFive.setText(seletcMonths.get(4));
        sbOne.setOnSeekBarChangeListener(this);
        sbTwo.setOnSeekBarChangeListener(this);
        sbThree.setOnSeekBarChangeListener(this);
        sbFour.setOnSeekBarChangeListener(this);
        sbFive.setOnSeekBarChangeListener(this);
    }


    private float getProgress(int i) {
        float progress = 0;
        switch (i % 5) {
            case 0:
                progress = (float) sbOne.getProgress();
                break;
            case 1:
                progress = (float) sbTwo.getProgress();
                break;
            case 2:
                progress = (float) sbThree.getProgress();
                break;
            case 3:
                progress = (float) sbFour.getProgress();
                break;
            case 4:
                progress = (float) sbFive.getProgress();
                break;
        }
        return progress;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        float values = (float) (seekBar.getProgress());
        switch (seekBar.getId()) {
            case R.id.sb_one:
                for (int i = 0; i < 112; i++) {
                    if (i % 5 == 0)
                        sumData.set(i, values);
                }
                mLineChart.setSumData(sumData, seletcMonths);
                break;
            case R.id.sb_two:
                for (int i = 0; i < 112; i++) {
                    if (i % 5 == 1)
                        sumData.set(i, values);
                }
                mLineChart.setSumData(sumData, seletcMonths);
                break;
            case R.id.sb_three:
                for (int i = 0; i < 112; i++) {
                    if (i % 5 == 2)
                        sumData.set(i, values);
                }
                mLineChart.setSumData(sumData, seletcMonths);
                break;
            case R.id.sb_four:
                for (int i = 0; i < 112; i++) {
                    if (i % 5 == 3)
                        sumData.set(i, values);
                }
                mLineChart.setSumData(sumData, seletcMonths);
                break;
            case R.id.sb_five:
                for (int i = 0; i < 112; i++) {
                    if (i % 5 == 4)
                        sumData.set(i, values);
                }
                mLineChart.setSumData(sumData, seletcMonths);
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

}
